package cc.rome753.wat;

import android.content.Context;
import android.media.AudioManager;
import android.util.Log;

import java.io.FileNotFoundException;
import java.io.FileReader;

/**
 * Created by YFB on 2018/11/16.
 */

public class AudiosManager  {
    private AudioManager audioManager;


    public void start(Context context) {
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setMode(AudioManager.MODE_NORMAL);
        audioManager.setSpeakerphoneOn(true);

    }

    /**
     * 切换听筒模式
     */
    public void setSpeakerPhoneOn(boolean on) {
        if (on) {
            Log.e("ttt", "外放外放");
//            audioManager.setMode(AudioManager.MODE_NORMAL);
            audioManager.setSpeakerphoneOn(true);
        } else {
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            audioManager.setSpeakerphoneOn(false);
        }

    }
    private static final String HEADSET_STATE_PATH = "/sys/class/switch/h2w/state";
    private boolean isHeadsetExists() {//白色医护判断耳机是否插拔
        char[] buffer = new char[1024];

        int newState = 0;

        try {
            FileReader file = new FileReader(HEADSET_STATE_PATH);
            int len = file.read(buffer, 0, 1024);
            newState = Integer.valueOf((new String(buffer, 0, len)).trim());
        }
        catch (FileNotFoundException e) {
            Log.e("FMTest", "This kernel does not have wired headset support");
        }
        catch (Exception e) {
            Log.e("FMTest", "", e);
        }
        return newState != 0;
    }

}
